package top.hkyzf.study.leetcode.stack;

import org.junit.Test;

import java.util.Stack;

/**
 * @author 朱峰
 * @date 2022-2-18 9:30
 */
public class 有效的括号_20 {
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if (stack.empty()) {
                stack.push(ch);
            } else {
                if (stack.peek() == ch-2 || stack.peek() == ch-1) {
                    stack.pop();
                } else {
                    stack.push(ch);
                }
            }
        }
        return stack.empty();
    }

    @Test
    public void testIsValid () {
        String s = "{}[]()";
        for (int i = 0; i < s.length(); i++) {
            int c = s.charAt(i);
            System.out.println(c);
        }
        System.out.println(isValid(s));
    }
}
